﻿using FlyIL.Misc;
using FlyIL.Trace;
using System.Collections.Generic;

namespace FlyIL.Optimizer;




class LeaPass : SimplfyRoot
{

    /// <summary>
    ///  是一个 lea 语句， 且不是用来 修改栈 
    /// </summary>
    public override bool IsMatch(Context con, Inst node, ExpByTrace exp = null)
        => node.Opt == Opt.lea && node.O1.Base != con.CodeSp;

    public override void Do(Context con, Inst node, ExpByTrace expression = null)
    {

        var exp = con.TraceStrict(node, ref node.O1, null); 

        if(exp != null)
        {
            if(exp.Calculable)
            {
                node.mov(node.O1, Operand.Make(exp.Result, node.O1.ByteSize));  
            }


            con.RemoveDeadCode(node, ref node.O1);
        }


    }
}
